<?php


namespace app\zhongcan\controller;


use app\zhongcan\model\NewsModel;
use app\zhongcan\validate\NewsValidate;
use cmf\controller\AdminBaseController;

class NewsController extends AdminBaseController
{
    //列表
    public function index()
    {
        $keyword = input('keyword', '');
        $where = [];
        if ($keyword) {
            $where[] = ['title|author', 'like', '%' . $keyword . '%'];
        }
        $list = NewsModel::order('list_order', 'desc')
            ->where($where)
            ->paginate(2);
        $this->assign('news', $list);
        return $this->fetch();
    }

    //显示添加页面
    public function add()
    {
        return $this->fetch();
    }

    //添加的提交
    public function addPost()
    {
        //接收数据
        $data = input('post.');
        //第一次处理
//        $content1 = htmlspecialchars_decode($data['content']);
//        //第二次处理(加true)
//        $content2 = cmf_replace_content_file_url($content1, true);
//        //第三次处理(还原)
//        $content3 = htmlspecialchars($content2);
//        $data['content'] = $content3;
        //验证数据（验证器)
        $validate = new NewsValidate();
        $res = $validate->scene('add')->check($data);
        if (!$res) {
            $this->error($validate->getError());
        }

        //然后入库
        if (NewsModel::create($data)) {
            $this->success('添加成功','News/index');
        } else {
            $this->success('添加失败');
        }
    }

    //显示编辑页面
    public function edit()
    {
        $id = input('id/d');
        $news = NewsModel::find($id);
        //第一次处理
        $content1 = htmlspecialchars_decode($news['content']);
        //第二次处理
        $content2 = cmf_replace_content_file_url($content1, true);
        $news['content'] = $content2;
        return $this->fetch('edit', [
            'news' => $news
        ]);
    }

    //添加的提交
    public function editPost()
    {
        //接收数据
        $data = input('post.');
        //第一次处理
        $content1 = htmlspecialchars_decode($data['content']);
        //第二次处理(加true)
        $content2 = cmf_replace_content_file_url($content1, true);
        //第三次处理(还原)
        $content3 = htmlspecialchars($content2);
        $data['content'] = $content3;
        //验证数据（验证器)
        $validate = new NewsValidate();
        $res = $validate->scene('edit')->check($data);
        if (!$res) {
            $this->error($validate->getError());
        }
        //先查询数据
        $news = NewsModel::find($data['id']);
        if(!$news){
            $this->error('未查询到数据');
        }
        $news->title = $data['title'];
        $news->image = $data['image'];
        $news->author = $data['author'];
        $news->look_num = $data['look_num'];
        $news->abstract = $data['abstract'];
        $news->content = $data['content'];
            //然后入库
            if ($news->save()) {
                $this->success('保存成功','News/index');
            } else {
                $this->success('保存失败');
            }
        }
        public function del()
        {
            $id = input('id/d');
            $news = NewsModel::destroy($id);
            if ($news) {
            $this->success('删除成功','News/index');
        } else {
            $this->success('删除失败');
        }
    }
    public function order()
    {

        parent::listOrders(new NewsModel());
        $this->success('排序成功');
    }
}